<?php

namespace common\modules\article\models\backend;

use common\widgets\grid\ActionColumn;
use common\widgets\grid\FormulaColumn;
use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;

/**
 * ItemSearch represents the model behind the search form about `common\modules\article\models\backend\Item`.
 */
class ItemSearch extends Item
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'category_id', 'views', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
            [['title', 'image', 'description', 'text', 'slug', 'status'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/article', 'Item List');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Item::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'          => $this->id,
            'category_id' => $this->category_id,
            'views'       => $this->views,
            'created_at'  => $this->created_at,
            'updated_at'  => $this->updated_at,
            'created_by'  => $this->created_by,
            'updated_by'  => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'image', $this->image])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'text', $this->text])
            ->andFilterWhere(['like', 'slug', $this->slug])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        /** @var array $statusValues */
        $statusValues = ArrayHelper::getValue($this->statusWorkflow(), 'status');

        return [
            [
                'attribute' => 'id',
                'filterOptions' => [
                    'style' => 'width:90px'
                ],
            ],

            [
                'attribute' => 'category_id',
                'value' => 'category.name',
                'vAlign'=>'middle',
                'width'=>'160px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => CategoryTree::getTreeList(),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择目录'],
                'format' => 'raw',
            ],

            [
                'attribute' => 'title',
                'width' => '300px',
            ],

            [
                'attribute' => 'views',
                'filterOptions' => [
                    'style' => 'width:90px'
                ],
            ],

            [
                'attribute' => 'status',
                'value' => function ($model) use ($statusValues) {
                    if (isset($statusValues[$model->status]))
                    {
                        return ArrayHelper::getValue($statusValues[$model->status], 'label');
                    }
                    return $model->status;
                },
                'vAlign'=>'middle',
                'width'=>'150px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => ArrayHelper::map($statusValues, 'value', 'label'),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择状态'],
                'format' => 'raw',
            ],

            [
                'class' => FormulaColumn::className(),
                'attribute' => 'created_at',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'width' => '200px',
                'hAlign' => GridView::ALIGN_CENTER,
                'mergeHeader' => true,
            ],

            [
                'class' => ActionColumn::className(),
            ],
        ];
    }
}
